#include "logger.h"

/****************************************************//**
 * Project-Namespace
 ********************************************************/
namespace jae
{

/****************************************************//**
 * This static method delete the logfile.
 ********************************************************/
void Logger::createNewLogFile()
{
    //Open a new empty logfile: 
    _logFile.open(LOG_NAME, std::ios::out | std::ios::trunc);
    
    if (_logFile.is_open())
    {
        _logFile << "*********************************************************************\n"
                 << "**                  JUST ANOTHER ENGINE - Logfile                  **\n"
                 << "*********************************************************************\n"
                 << "Version: " << BUILD_VERSION << " " << BUILD_STATE << " ("
                 << BUILD_DATE << "." << BUILD_MONTH << "." << BUILD_YEAR << ")\n\n";
    
        _logFile.flush(); //writes the content of the buffer to file
        _logFile.close();
    }
    else
    {
        perror("Unable to open logfile!");
    }
}

/****************************************************//**
 * Constructor of the class. Creates a new file.
 ********************************************************/
Logger::Logger()
{
    //Nothing to do, yet
}

/****************************************************//**
 * This private method return true if logfile is in app mode.
 ********************************************************/
bool Logger::open()
{
  _logFile.open(LOG_NAME, std::ios::out | std::ios::app);
  
  if (_logFile.is_open())
  {
      return true;
  }
  else
  {
      perror("Unable to open logfile!");
      return false;
  }
}

/****************************************************//**
 * This method tries to close a open file.
 ********************************************************/
void Logger::close()
{
  if (_logFile.is_open())
  {
      _logFile.flush(); //writes the content of the buffer to file
      _logFile.close();
  }
}

/****************************************************//**
 * This method writes the systemtime to the logfile.
 ********************************************************/
const char* Logger::getTime(void)
{
      time_t currentTime;
      time ( &currentTime );
      
      return ctime(&currentTime);
}

} //namespace jae
